<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- data必须是一个数组 -->
      <el-table
      stripe
      border
      :data="cateList"
      style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        width="50px">
      </el-table-column>
      <el-table-column
        prop="cate_name"
        label="分类名称"
        >
      </el-table-column>
      <el-table-column
        prop="cate_alias"
        label="分类别名">
      </el-table-column>
       <el-table-column label="操作">
         <template v-slot='scope'>
          <el-button size="mini" type="primary" @click="hCheck(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="hDel(scope.row.id)">删除</el-button>
         </template>

      </el-table-column>
    </el-table>
    </el-card>

    <el-dialog
  title="添加文章分类"
  :visible.sync="addVisible"
  width="35%"
  :close-on-click-modal='false'
  @closed="$refs.addRef.resetFields()"
  >
  <!-- 添加表单 -->

 <span>
        <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
        </el-form>
</span>
  <span slot="footer" class="dialog-footer">
    <el-button size="mini" @click="addVisible = false">取 消</el-button>
    <el-button size="mini" type="primary" @click="hSubmit">确 定</el-button>
  </span>
</el-dialog>
    <el-dialog
  title="修改文章分类"
  :visible.sync="EditVisible"
  width="35%"
  :close-on-click-modal='false'
  @closed="$refs.EditRef.resetFields()"
  >
  <!-- 修改表单 -->

 <span>
        <el-form :model="EditForm" :rules="addRules" ref="EditRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="EditForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="EditForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
        </el-form>
</span>
  <span slot="footer" class="dialog-footer">
    <el-button size="mini" @clickEdit ="EditVisible= false">取 消</el-button>
    <el-button size="mini" type="primary" @click="hEdit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      addVisible: false,
      EditVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      EditForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }

    }
  },
  created () {
    this.loadH()
  },
  methods: {
    // 像服务器发请求获取列表数据，在created里调用
    async loadH () {
      const { data: res } = await this.$axios.get('/my/cate/list')
      console.log(res)
      if (res.code !== 0) return
      this.cateList = res.data
    },
    // 添加列表
    hSubmit () {
      // 兜底校验
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        // 如果成功就发送请求
        const { data: res } = await this.$axios.post('/my/cate/add', this.addForm)
        // 提醒用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 关闭错误
        this.addVisible = false
        // 重新获取列表
        this.loadH()
      })
    },
    // 修改列表请求
    hEdit () {
      this.EditVisible = false
      this.$refs.EditRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.put('/my/cate/info', this.EditForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)

        this.loadH()
      })
    },
    hCheck (row) {
      if (row.id === 1 || row.id === 2) return this.$message.warning('管理员禁止修改')
      this.EditVisible = true
      this.EditForm = { ...row }
    },
    // 删除列表请求
    hDel (id) {
      if (id === 1 || id === 2) return this.$message.warning('不允许删除此分类')
      this.$confirm('您确定要删除吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        // 根据id删除列表
        // 向服务器发送请求注意delete的参数params:{}和get一样
        const { data: res } = await this.$axios.delete('/my/cate/del', { params: { id } })
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 删除后重新更新列表
        this.loadH()
      }).catch(() => {})
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
